import tensorflow as tf
import numpy as np
import pandas
import matplotlib.pyplot as plt

if __name__ == '__main__':

    lr = tf.Variable(1e-4)

    data = pandas.read_csv('../data/sample/LogiReg_data.txt', header=None, names=["exam1", "exam2", "abroad"])
    x = np.array(list(zip(data["exam1"], data["exam2"]))).astype(np.float32)
    x_input = tf.Variable(x, dtype=tf.float32)
    tmp = data["abroad"].values.reshape(100, 1)
    y_input = tf.Variable(tmp, dtype=tf.float32)

    w = tf.Variable(tf.random_normal([2, 10]), dtype=tf.float32)
    b = tf.Variable(tf.zeros([1, 10]), dtype=tf.float32)
    y = tf.nn.sigmoid(tf.matmul(x_input, w) + b)

    w2 = tf.Variable(tf.random_normal([10, 1]), dtype=tf.float32)
    b2 = tf.Variable(tf.zeros([1, 1]), dtype=tf.float32)
    y2 = tf.nn.sigmoid(tf.matmul(y, w2) + b2)

    loss = tf.reduce_mean(tf.square(y_input - y2))
    train = tf.train.AdamOptimizer(lr).minimize(loss)

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        for i in range(30000):
            session.run(train)
            if i % 200 == 0:
                print(session.run(loss))
            if i % 1000 == 0:
                tf.assign(lr, lr / 4)

        # print(session.run(y2))
        # print([1 if x > 0.5 else 0 for x in session.run(tf.squeeze(y2))])

        for step in range(1, 9, 1):
            step = step * 0.1
            tmp = [1 if x > step else 0 for x in session.run(tf.squeeze(y2))]
            cnt = 0
            print(tmp)
            for i in range(len(tmp)):
                if tmp[i] == data["abroad"][i]:
                    cnt += 1
            print("thread hold:", step, "准确值：", cnt / len(tmp))
